﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using CallGraphExtractor;

namespace WinFormsDemoHost
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void mLoadButton_Click(object sender, EventArgs e)
        {
            mMessagesListBox.Items.Clear();

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "DLLs (.dll)|*.dll|Executables (*.exe)|*.exe";
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Extractor extr = new Extractor();
                extr.ResultPackSize = 50;
                extr.AssemblyPaths.Add(new Uri(openFileDialog.FileName, UriKind.Absolute));
                extr.ExtractionSubresultExtracted +=
                    new EventHandler<SubresultEventArgs<CallGraphEdgeCollection>>(
                        extr_ExtractionSubresultExtracted);
                extr.ExtractionDone += new EventHandler<RunWorkerCompletedEventArgs>(extr_ExtractionDone);
                extr.ExtractAsync();
            }
        }

        #region Extraction Handler

        delegate void SubresultHandler(SubresultEventArgs<CallGraphEdgeCollection> e, ListBox lb);
        SubresultHandler dExtr_SubresultHandler = new SubresultHandler(extr_SubresultHandler);

        void extr_ExtractionSubresultExtracted(object sender, SubresultEventArgs<CallGraphEdgeCollection> e)
        {
            this.BeginInvoke(dExtr_SubresultHandler, e, mMessagesListBox);
        }

        static void extr_SubresultHandler(SubresultEventArgs<CallGraphEdgeCollection> e, ListBox lb)
        {
            foreach (var item in e.Subresult)
            {
                lb.Items.Add("CALLER: " + item.Caller.MemberReference.ToString());
                foreach (var called in item.Called)
                {
                    lb.Items.Add("->    " + called.ExtendedMemberReference.MemberReference.ToString());
                }
            }
        }

        static void extr_ExtractionDone(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true || e.Error != null)
            {
                if (e.Cancelled == true)
                    MessageBox.Show("Extraction cancelled");
                if (e.Error != null)
                    MessageBox.Show(e.Error.Message);
            }
            else
                MessageBox.Show("Extraction done");
        }

        #endregion Extraction Handler

        private void mGetMembersButton_Click(object sender, EventArgs e)
        {
            mMessagesListBox.Items.Clear();

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "DLLs (.dll)|*.dll|Executables (*.exe)|*.exe";
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Extractor extr = new Extractor();
                extr.ResultPackSize = 1000;
                extr.AssemblyPaths.Add(new Uri(openFileDialog.FileName, UriKind.Absolute));
                extr.GetMembersSubresultExtracted +=
                    new EventHandler<SubresultEventArgs<ExtendedMemberReference>>(
                        extr_GetMembersSubresultExtracted);
                extr.GetMembersDone += new EventHandler<RunWorkerCompletedEventArgs>(extr_GetMembersDone);
                extr.GetMembersAsync();
            }
        }

        #region Get members Handler

        delegate void GetMembersSubresultHandler(SubresultEventArgs<ExtendedMemberReference> e, ListBox lb);
        GetMembersSubresultHandler dGetMem_SubresultHandler =
            new GetMembersSubresultHandler(extr_GetMembersSubresultHandler);

        void extr_GetMembersSubresultExtracted(object sender, SubresultEventArgs<ExtendedMemberReference> e)
        {
            this.BeginInvoke(dGetMem_SubresultHandler, e, mMessagesListBox);
        }

        static void extr_GetMembersSubresultHandler(SubresultEventArgs<ExtendedMemberReference> e, ListBox lb)
        {
            foreach (var item in e.Subresult)
            {
                lb.Items.Add(item.MemberReference.ToString());
            }
        }

        void extr_GetMembersDone(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true || e.Error != null)
            {
                if (e.Cancelled == true)
                    MessageBox.Show("Extraction cancelled");
                if (e.Error != null)
                    MessageBox.Show(e.Error.Message);
            }
            else
                MessageBox.Show("Get members done");
        }

        #endregion Get members Handler

        private void mGetTypesButton_Click(object sender, EventArgs e)
        {
            mMessagesListBox.Items.Clear();

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "DLLs (.dll)|*.dll|Executables (*.exe)|*.exe";
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Extractor extr = new Extractor();
                extr.ResultPackSize = 1000;
                extr.AssemblyPaths.Add(new Uri(openFileDialog.FileName, UriKind.Absolute));
                extr.GetTypesSubresultExtracted +=
                    new EventHandler<SubresultEventArgs<ExtendedType>>(extr_GetTypesSubresultExtracted);
                extr.GetTypesDone += new EventHandler<RunWorkerCompletedEventArgs>(extr_GetTypesDone);
                extr.GetTypesAsync();
            }
        }

        #region Get types Handler

        delegate void GetTypesSubresultHandler(SubresultEventArgs<ExtendedType> e, ListBox lb);
        GetTypesSubresultHandler dGetTypes_SubresultHandler =
            new GetTypesSubresultHandler(extr_GetTypesSubresultHandler);

        void extr_GetTypesSubresultExtracted(object sender, SubresultEventArgs<ExtendedType> e)
        {
            this.BeginInvoke(dGetTypes_SubresultHandler, e, mMessagesListBox);
        }

        static void extr_GetTypesSubresultHandler(SubresultEventArgs<ExtendedType> e, ListBox lb)
        {
            foreach (ExtendedType item in e.Subresult)
            {
                lb.Items.Add(item.Type.ToString() + (item.OuterType != null ? " nested in " + item.OuterType.ToString() : ""));
            }
        }

        void extr_GetTypesDone(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true || e.Error != null)
            {
                if (e.Cancelled == true)
                    MessageBox.Show("Extraction cancelled");
                if (e.Error != null)
                    MessageBox.Show(e.Error.Message);
            }
            else
                MessageBox.Show("Get types done");
        }

        #endregion Get types Handler

        private void mGetModulesButton_Click(object sender, EventArgs e)
        {
            mMessagesListBox.Items.Clear();

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "DLLs (.dll)|*.dll|Executables (*.exe)|*.exe";
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Extractor extr = new Extractor();
                extr.ResultPackSize = 1000;
                extr.AssemblyPaths.Add(new Uri(openFileDialog.FileName, UriKind.Absolute));
                extr.GetModulesSubresultExtracted +=
                    new EventHandler<SubresultEventArgs<Reflector.CodeModel.IModule>>(
                        extr_GetModulesSubresultExtracted);
                extr.GetModulesDone += new EventHandler<RunWorkerCompletedEventArgs>(extr_GetModulesDone);
                extr.GetModulesAsync();
            }
        }

        #region Get modules Handler

        delegate void GetModulesSubresultHandler(SubresultEventArgs<Reflector.CodeModel.IModule> e, ListBox lb);
        GetModulesSubresultHandler dGetModules_SubresultHandler =
            new GetModulesSubresultHandler(extr_GetModulesSubresultHandler);

        void extr_GetModulesSubresultExtracted(object sender, SubresultEventArgs<Reflector.CodeModel.IModule> e)
        {
            this.BeginInvoke(dGetModules_SubresultHandler, e, mMessagesListBox);
        }

        static void extr_GetModulesSubresultHandler(SubresultEventArgs<Reflector.CodeModel.IModule> e,
            ListBox lb)
        {
            foreach (var item in e.Subresult)
            {
                lb.Items.Add(item.Name);
            }
        }

        void extr_GetModulesDone(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true || e.Error != null)
            {
                if (e.Cancelled == true)
                    MessageBox.Show("Extraction cancelled");
                if (e.Error != null)
                    MessageBox.Show(e.Error.Message);
            }
            else
                MessageBox.Show("Get modules done");
        }

        #endregion Get modules Handler

        private void mGetAssembliesButton_Click(object sender, EventArgs e)
        {
            mMessagesListBox.Items.Clear();

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "DLLs (.dll)|*.dll|Executables (*.exe)|*.exe";
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Extractor extr = new Extractor();
                extr.ResultPackSize = 1000;
                extr.AssemblyPaths.Add(new Uri(openFileDialog.FileName, UriKind.Absolute));
                extr.GetAssembliesSubresultExtracted +=
                    new EventHandler<SubresultEventArgs<Reflector.CodeModel.IAssembly>>(
                        extr_GetAssembliesSubresultExtracted);
                extr.GetAssembliesDone += new EventHandler<RunWorkerCompletedEventArgs>(extr_GetAssembliesDone);
                extr.GetAssembliesAsync();
            }
        }

        #region Get assemblies Handler

        delegate void GetAssembliesSubresultHandler(SubresultEventArgs<Reflector.CodeModel.IAssembly> e,
            ListBox lb);
        GetAssembliesSubresultHandler dGetAssemblies_SubresultHandler =
            new GetAssembliesSubresultHandler(extr_GetAssembliesSubresultHandler);

        void extr_GetAssembliesSubresultExtracted(object sender,
            SubresultEventArgs<Reflector.CodeModel.IAssembly> e)
        {
            this.BeginInvoke(dGetAssemblies_SubresultHandler, e, mMessagesListBox);
        }

        static void extr_GetAssembliesSubresultHandler(SubresultEventArgs<Reflector.CodeModel.IAssembly> e,
            ListBox lb)
        {
            foreach (var item in e.Subresult)
            {
                lb.Items.Add(item.Name);
            }
        }

        void extr_GetAssembliesDone(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true || e.Error != null)
            {
                if (e.Cancelled == true)
                    MessageBox.Show("Extraction cancelled");
                if (e.Error != null)
                    MessageBox.Show(e.Error.Message);
            }
            else
                MessageBox.Show("Get assemblies done");
        }

        #endregion Get assemblies Handler

        private void mGetNamespacesButton_Click(object sender, EventArgs e)
        {
            mMessagesListBox.Items.Clear();

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "DLLs (.dll)|*.dll|Executables (*.exe)|*.exe";
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Extractor extr = new Extractor();
                extr.ResultPackSize = 1000;
                extr.AssemblyPaths.Add(new Uri(openFileDialog.FileName, UriKind.Absolute));
                extr.GetNamespacesSubresultExtracted +=
                    new EventHandler<SubresultEventArgs<string>>(extr_GetNamespacesSubresultExtracted);
                extr.GetNamespacesDone += new EventHandler<RunWorkerCompletedEventArgs>(extr_GetNamespacesDone);
                extr.GetNamespacesAsync();
            }
        }

        #region Get namespace Handler

        delegate void GetNamespacesSubresultHandler(SubresultEventArgs<string> e, ListBox lb);
        GetNamespacesSubresultHandler dGetNamespaces_SubresultHandler =
            new GetNamespacesSubresultHandler(extr_GetNamespacesSubresultHandler);

        void extr_GetNamespacesSubresultExtracted(object sender, SubresultEventArgs<string> e)
        {
            this.BeginInvoke(dGetNamespaces_SubresultHandler, e, mMessagesListBox);
        }

        static void extr_GetNamespacesSubresultHandler(SubresultEventArgs<string> e, ListBox lb)
        {
            foreach (var item in e.Subresult)
            {
                lb.Items.Add(item);
            }
        }

        void extr_GetNamespacesDone(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true || e.Error != null)
            {
                if (e.Cancelled == true)
                    MessageBox.Show("Extraction cancelled");
                if (e.Error != null)
                    MessageBox.Show(e.Error.Message);
            }
            else
                MessageBox.Show("Get namespaces done");
        }

        #endregion Get namespace Handler
    }
}
